package com.changpay.zywy.ui.activity.machinecontrol

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.AllowLogAdapter
import com.changpay.zywy.base.BaseFragment
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.MyAllocaLogBean
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.loan.FinishAccountActivity

import kotlinx.android.synthetic.main.fragment_refresh_layout.*


class AllowDetailNewFragment : BaseFragment(), IView {
    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {

    }

    override fun showMsg(msg: String) {

    }

    override fun showError(errorMsg: String) {

    }

    private var position: Int = 0  // 当前viewPage的位置
    private lateinit var type: String
    private var page = 1
    private val pagesize = 10


    companion object {
        fun getInstance(instanceType: Int): AllowDetailNewFragment {
            val bundle = Bundle()
            bundle.putInt("position", instanceType)
            val fragment = AllowDetailNewFragment()
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun attachLayoutRes(): Int = R.layout.fragment_refresh_layout

    override fun lazyLoad() {
        loadData()

    }

    override fun initView(view: View) {
        position = arguments?.getInt("position") ?: 0
        type = if (position == 0) "1" else "2"
        swipeRefreshLayout.isEnabled = false
        initRecyclerView()
    }

    /**
     * datas
     */
    private val datas_one = mutableListOf<MyAllocaLogBean>()

    /**
     * datas
     */
    private val datas_two = mutableListOf<MyAllocaLogBean>()


    /**
     * Adapter
     */
    private val mAdapter_one: AllowLogAdapter by lazy {
        AllowLogAdapter(context, datas_one)
    }

    /**
     * Adapter
     */
    private val mAdapter_two: AllowLogAdapter by lazy {
        AllowLogAdapter(context, datas_two)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }

    /**
     * LoadMoreListener
     */
    private val onRequestLoadMoreListener = BaseQuickAdapter.RequestLoadMoreListener {
        when (position) {
            0 -> {
                page = mAdapter_one.data.size / pagesize + 1
                loadData()
            }
            1 -> {
                page = mAdapter_two.data.size / pagesize + 1
                loadData()
            }
        }

    }

    fun loadData() {
        if (position == 0) {
            val param = hashMapOf<String, String>()
            param["key"] = token
            param["page"] = page.toString()
            param["pagesize"] = pagesize.toString()
            param["type"] = type
            RetrofitHelper.service.allocationLogList(param).sss(this)
            {
                mAdapter_one.run {
                    addData(it.result)
                    val size = it.result.size
                    if (size < 10) {
                        loadMoreEnd(true)
                    } else {
                        loadMoreComplete()
                    }
                }
            }
        } else {
            val param = hashMapOf<String, String>()
            param["key"] = token
            param["page"] = page.toString()
            param["pagesize"] = pagesize.toString()
            param["type"] = type
            RetrofitHelper.service.allocationLogList(param).sss(this)
            {
                mAdapter_two.run {
                    addData(it.result)
                    val size = it.result.size
                    if (size < 10) {
                        loadMoreEnd(true)
                    } else {
                        loadMoreComplete()
                    }
                }
            }
        }
    }

    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        when (this.position) {
            0 -> if (datas_one.size != 0) {
                val data = datas_one[position]
                if (data.dt_type == "1") {
                    Intent(activity, AllocationDetailActivity::class.java).run {
                        putExtra("id", data.id.toString())
                        startActivity(this)
                    }

                } else { Intent(activity, FinishAccountActivity::class.java).run {
                        type = "0"
                        putExtra("allot_id", data.id.toString())
                        startActivity(this)
                    } }
            }
            1 -> if (datas_two.size != 0) {
                val data = datas_two[position]
                if (data.dt_type == "1") {
                    Intent(activity, AllocationDetailActivity::class.java).run {
                        putExtra("id", data.id.toString())
                        startActivity(this)
                    }

                } else { Intent(activity, FinishAccountActivity::class.java).run {
                    type = "0"
                    putExtra("allot_id", data.id.toString())
                    startActivity(this)
                } }
            }
        }
    }

    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            when (position) {
                0 -> adapter = mAdapter_one
                1 -> adapter = mAdapter_two
            }
            itemAnimator = DefaultItemAnimator()
        }
        val adapter = when (position) {
            0 -> mAdapter_one
            1 -> mAdapter_two
            else -> mAdapter_one
        }
        adapter.run {
            onItemClickListener = this@AllowDetailNewFragment.onItemClickListener
            bindToRecyclerView(recyclerView)
            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            setEmptyView(R.layout.fragment_empty_layout)
        }
    }


}






